package ApplicationStartTest.KafkaConnect.entry;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class Sender {

    @Autowired
    private KafkaTemplate kafkaTemplate;
    private Gson gson = new GsonBuilder().create();
    //gson.tojson

    @Value("${spring.kafka.template.default-topic}")
    private String topic;

    public void sendMessage(Message message){

        //设置kafkaTemplate 监听器
        //kafkaTemplate.setProducerListener
        kafkaTemplate.send(this.topic, gson.toJson(message));
    }

}
